<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="药品编码" prop="drugCode">
        <el-input
          v-model="queryParams.drugCode"
          placeholder="请输入药品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药品名称" prop="drugName">
        <el-input
          v-model="queryParams.drugName"
          placeholder="请输入药品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="规格" prop="specification">-->
<!--        <el-input-->
<!--          v-model="queryParams.specification"-->
<!--          placeholder="请输入规格"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="剂型" prop="dosageForm">-->
<!--        <el-input-->
<!--          v-model="queryParams.dosageForm"-->
<!--          placeholder="请输入剂型"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="单位" prop="unit">-->
<!--        <el-input-->
<!--          v-model="queryParams.unit"-->
<!--          placeholder="请输入单位"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="单价" prop="price">-->
<!--        <el-input-->
<!--          v-model="queryParams.price"-->
<!--          placeholder="请输入单价"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="生产厂家" prop="manufacturer">
        <el-input
          v-model="queryParams.manufacturer"
          placeholder="请输入生产厂家"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作类型" prop="operationType">
        <el-select
          v-model="queryParams.operationType"
          placeholder="请选择操作类型"
          clearable
          @change="handleQuery"
        >
          <el-option label="入库" value="1"></el-option>
          <el-option label="出库" value="2"></el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="数量" prop="quantity">-->
<!--        <el-input-->
<!--          v-model="queryParams.quantity"-->
<!--          placeholder="请输入数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="操作后库存余额" prop="balance">-->
<!--        <el-input-->
<!--          v-model="queryParams.balance"-->
<!--          placeholder="请输入操作后库存余额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="批次号" prop="batchNumber">-->
<!--        <el-input-->
<!--          v-model="queryParams.batchNumber"-->
<!--          placeholder="请输入批次号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="生产日期" prop="productionDate">
        <el-date-picker clearable
                        v-model="queryParams.productionDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择生产日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="有效期至" prop="expiryDate">
        <el-date-picker clearable
                        v-model="queryParams.expiryDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择有效期至">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="仓库号" prop="warehouseId">
        <el-input
          v-model="queryParams.warehouseId"
          placeholder="请输入仓库号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="操作时间" prop="operationTime">-->
<!--        <el-date-picker clearable-->
<!--                        v-model="queryParams.operationTime"-->
<!--                        type="date"-->
<!--                        value-format="yyyy-MM-dd"-->
<!--                        placeholder="请选择操作时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item label="操作人" prop="operator">
        <el-input
          v-model="queryParams.operator"
          placeholder="请输入操作人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="关联单号" prop="referenceNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.referenceNo"-->
<!--          placeholder="请输入关联单号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['his:inventory:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['his:inventory:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['his:inventory:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['his:inventory:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inventoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="药品编码" align="center" prop="drugCode"/>
      <el-table-column label="药品名称" align="center" prop="drugName"/>
      <el-table-column label="规格" align="center" prop="specification"/>
      <el-table-column label="剂型" align="center" prop="dosageForm"/>
      <el-table-column label="单位" align="center" prop="unit"/>
      <el-table-column label="单价" align="center" prop="price"/>
      <el-table-column label="生产厂家" align="center" prop="manufacturer"/>
      <el-table-column label="操作类型" align="center" prop="operationType">
        <template slot-scope="scope">
          {{ scope.row.operationType === '1' ? '入库' : '出库' }}
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center" prop="quantity"/>
      <el-table-column label="操作后库存余额" align="center" prop="balance"/>
      <el-table-column label="批次号" align="center" prop="batchNumber"/>
      <el-table-column label="生产日期" align="center" prop="productionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.productionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期至" align="center" prop="expiryDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expiryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="仓库号" align="center" prop="warehouseId"/>
      <el-table-column label="操作时间" align="center" prop="operationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.operationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="operator"/>
      <el-table-column label="关联单号" align="center" prop="referenceNo"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['his:inventory:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['his:inventory:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改药品库存记录对话框 -->
    <!-- 添加或修改药品库存记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body class="custom-dialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" label-position="right">
        <el-row :gutter="20">
          <!-- 左列 -->
          <el-col :span="12">
            <el-form-item label="药品编码：" prop="drugCode">
              <el-input v-model="form.drugCode" placeholder="请输入" class="input-width"/>
            </el-form-item>
            <el-form-item label="药品名称：" prop="drugName">
              <el-input v-model="form.drugName" placeholder="请输入" class="input-width"/>
            </el-form-item>
            <el-form-item label="规格：" prop="specification">
              <el-input v-model="form.specification" placeholder="请输入" class="input-width"/>
            </el-form-item>
            <el-form-item label="剂型：" prop="dosageForm">
              <el-input v-model="form.dosageForm" placeholder="请输入" class="input-width"/>
            </el-form-item>
            <el-form-item label="单位：" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入" class="input-width"/>
            </el-form-item>
            <el-form-item label="单价：" prop="price">
              <el-input v-model="form.price" placeholder="请输入" class="input-width">
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
            <el-form-item label="生产厂家：" prop="manufacturer">
              <el-input v-model="form.manufacturer" placeholder="请输入" class="input-width"/>
            </el-form-item>
            <el-form-item label="操作类型：" prop="operationType">
              <el-select
                v-model="form.operationType"
                placeholder="请选择"
                class="input-width"
                :disabled="!isAdd"
              >
                <el-option label="入库" value="1"/>
                <el-option label="出库" value="2"/>
              </el-select>
            </el-form-item>
          </el-col>

          <!-- 右列 -->
          <el-col :span="12">
            <el-form-item label="数量：" prop="quantity">
              <el-input v-model="form.quantity" placeholder="请输入" class="input-width"/>
            </el-form-item>
            <el-form-item label="库存余额：" prop="balance">
              <el-input v-model="form.balance" placeholder="请输入" class="input-width"/>
            </el-form-item>
            <el-form-item label="批次号：" prop="batchNumber">
              <el-input v-model="form.batchNumber" placeholder="请输入" class="input-width"/>
            </el-form-item>
            <el-form-item label="生产日期：" prop="productionDate">
              <el-date-picker
                v-model="form.productionDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期"
                class="input-width"
              />
            </el-form-item>
            <el-form-item label="有效期至：" prop="expiryDate">
              <el-date-picker
                v-model="form.expiryDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期"
                class="input-width"
              />
            </el-form-item>
            <el-form-item label="仓库号：" prop="warehouseId">
              <el-input v-model="form.warehouseId" placeholder="请输入" class="input-width"/>
            </el-form-item>
            <el-form-item label="操作时间：" prop="operationTime">
              <el-date-picker
                v-model="form.operationTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择时间"
                class="input-width"
              />
            </el-form-item>
            <el-form-item label="操作人：" prop="operator">
              <el-input v-model="form.operator" placeholder="请输入" class="input-width"/>
            </el-form-item>
            <el-form-item label="关联单号：" prop="referenceNo">
              <el-input v-model="form.referenceNo" placeholder="请输入" class="input-width"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注：" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            style="width: 100%"
          />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listInventory, getInventory, delInventory, addInventory, updateInventory} from "@/api/his/inventory";

export default {
  name: "Inventory",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 药品库存记录表格数据
      inventoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        drugCode: null,
        drugName: null,
        specification: null,
        dosageForm: null,
        unit: null,
        price: null,
        manufacturer: null,
        operationType: null,
        quantity: null,
        balance: null,
        batchNumber: null,
        productionDate: null,
        expiryDate: null,
        warehouseId: null,
        operationTime: null,
        operator: null,
        referenceNo: null,
      },
      // 表单参数
      form: {
        id: null,
        drugCode: null,
        drugName: null,
        specification: null,
        dosageForm: null,
        unit: null,
        price: null,
        manufacturer: null,
        operationType: null,
        quantity: null,
        balance: null,
        batchNumber: null,
        productionDate: null,
        expiryDate: null,
        warehouseId: null,
        operationTime: null,
        operator: null,
        referenceNo: null,
        remark: null
      },
      // 操作类型选项
      operationTypeOptions: [
        {value: '1', label: '入库'},
        {value: '2', label: '出库'}
      ],
      // 控制新增/修改状态
      isAdd: true,
      // 表单校验
      rules: {
        drugCode: [
          {required: true, message: "药品编码不能为空", trigger: "blur"}
        ],
        drugName: [
          {required: true, message: "药品名称不能为空", trigger: "blur"}
        ],
        operationType: [
          {required: true, message: "请选择操作类型", trigger: "change"}
        ],
        quantity: [
          {required: true, message: "数量不能为空", trigger: "blur"}
        ],
        balance: [
          {required: true, message: "操作后库存余额不能为空", trigger: "blur"}
        ],
        operationTime: [
          {required: true, message: "操作时间不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询药品库存记录列表 */
    getList() {
      this.loading = true;
      listInventory(this.queryParams).then(response => {
        this.inventoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        drugCode: null,
        drugName: null,
        specification: null,
        dosageForm: null,
        unit: null,
        price: null,
        manufacturer: null,
        operationType: null,
        quantity: null,
        balance: null,
        batchNumber: null,
        productionDate: null,
        expiryDate: null,
        warehouseId: null,
        operationTime: null,
        operator: null,
        referenceNo: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加药品库存记录";
      this.isAdd = true;
      // 默认选中入库
      this.form.operationType = '1';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInventory(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改药品库存记录";
        this.isAdd = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 确保操作类型存在
          if (this.isAdd && !this.form.operationType) {
            this.$message.error("请选择操作类型");
            return;
          }

          if (this.form.id != null) {
            updateInventory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInventory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除药品库存记录编号为"' + ids + '"的数据项？').then(function () {
        return delInventory(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('his/inventory/export', {
        ...this.queryParams
      }, `inventory_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.custom-dialog .el-dialog__body {
  padding: 20px 30px;
}

.input-width {
  width: 100%;
}

.el-form-item {
  margin-bottom: 18px;
}

.el-date-editor.el-input {
  width: 100%;
}

.el-select {
  width: 100%;
}

.dialog-footer {
  text-align: right;
  padding: 20px 30px 0;
  border-top: 1px solid #e8e8e8;
}
</style>
